<?php

class CategoryController extends BaseController {
	
	//admin
	public function showIndex(){
		$title = "Category Sabahkini";
		$data = Category::orderBy('id','desc')->paginate(Config::get('config.number_page'));		
		return View::make('admin.category.index')->with('datas',$data)
		->with('allStatus',Config::get('config.category_status'))
		->with('title',$title);

	}	
	public function postCreate()  {	
		 	$category = new Category();
			$category->name = Input::get('name');
			$category->LanguageID=Input::get('LanguageID');
			$category->description=Input::get('description');
			$category->status=Input::get('status');
			$rules = array('name'=> 'required|unique:categories');	
			$validator = Validator::make(Input::all(), $rules);
			if ($validator->fails()) {
				return Redirect::back()->withInput()->with('errMg', 'The Category name has already been taken, Please try again !'); 
			} else{				
				if($category->addNew()){
					return Redirect::to(Config::get('config.domain').'category')
						->withInput()->with('success', 'Category Created Successfully !'); 					
				}else{
					return Redirect::to(Config::get('config.domain').'category/add')
						->withInput()->with('errMg', 'Error, Please Try Again !'); 
				}
			}	
	}
	public function getEdit($id){
		$dataEdit = Category::find(intval($id)); 
		if($dataEdit){
			return View::make('admin.category.edit')->with('dataEdit',$dataEdit)
		       ->with('allStatus',Config::get('config.category_status'))
		       ->with('allLang',Language::get())
		       ->with('title','Edit Category Sabahkini');
		}else{
			return Redirect::to(Config::get('config.domain').'category')
				->withInput()->with('errMg', 'Error, Please Try Again !'); 
		}
       
	}
	public function update($id){
	    $category = Category::find(intval($id)); 		
        $category->name = Input::get('name');
        $category->LanguageID  = Input::get('LanguageID');
        $category->description   = Input::get('description');
        $category->status      = Input::get('status');      

        if($category->addNew()){
				return Redirect::to(Config::get('config.domain').'category')
					->withInput()->with('success', 'Category Edited Successfully !'); 					
			}else{
				return Redirect::to(Config::get('config.domain').'category/edit/'.$id); 
			}
	}
	public function changes($id){		
		$str = explode('-', $id);
		$check = false;
		if(count($str)==2){
			if(intval($str[0])&& in_array($str[1],Config::get('config.category_status'))){
				$category = Category::find(intval($str[0])); 
				foreach(Config::get('config.category_status') as $key=>$value){
						if($value==$str[1]){
							 $category->status = $key;
							 break;
						}
				}
				if($category->addNew()){
					$check = true;
				}  
			}
		}
		if($check==true){
			return Redirect::back()
				->withInput()->with('success', 'Status Changes Successfully !'); 
		}else{
			return Redirect::back()
				->withInput()->with('errMg', 'Error, Please Try Again !');
		}
		
	}
	public function destroy($id) {
        Category::destroy(intval($id)); 
        return Redirect::back()
        	->withInput()->with('success', 'Category Deleted Successfully !');
    }
	
}